package com.ruoyi.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.utils.SnowflakeIdGenerator;
import com.ruoyi.system.client.C5ApiClientService;
import com.ruoyi.system.client.impl.C5ApiClientServiceImpl;
import com.ruoyi.system.domain.c5.C5Account;
import com.ruoyi.system.domain.c5.GetSteamInfoResponse;
import com.ruoyi.system.mapper.C5AccountMapper;
import com.ruoyi.system.service.IC5AccountService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

import static com.ruoyi.common.utils.ShiroUtils.getUserId;

/**
 * C5Game账户Service业务层处理
 *
 * @author ruoyi
 * @date 2025-10-15
 */
@Service
public class C5AccountServiceImpl extends ServiceImpl<C5AccountMapper, C5Account> implements IC5AccountService {
    @Autowired
    private C5AccountMapper c5AccountMapper;
    @Autowired
    private C5ApiClientService c5ApiClientService;
    @Autowired
    private SnowflakeIdGenerator snowflakeIdGenerator;

    /**
     * 查询C5Game账户
     *
     * @param id C5Game账户主键
     * @return C5Game账户
     */
    @Override
    public C5Account selectC5AccountById(Long id) {
        return c5AccountMapper.selectC5AccountById(id);
    }

    /**
     * 查询C5Game账户列表
     *
     * @param c5Account C5Game账户
     * @return C5Game账户
     */
    @Override
    public List<C5Account> selectC5AccountList(C5Account c5Account) {
        return c5AccountMapper.selectC5AccountList(c5Account);
    }

    /**
     * 新增C5Game账户
     *
     * @param c5Account C5Game账户
     * @return 结果
     */
    @Override
    public int insertC5Account(C5Account c5Account) {
        return c5AccountMapper.insertC5Account(c5Account);
    }

    /**
     * 修改C5Game账户
     *
     * @param c5Account C5Game账户
     * @return 结果
     */
    @Override
    public int updateC5Account(C5Account c5Account) {
        return c5AccountMapper.updateC5Account(c5Account);
    }

    /**
     * 批量删除C5Game账户
     *
     * @param ids 需要删除的C5Game账户主键
     * @return 结果
     */
    @Override
    public int deleteC5AccountByIds(String ids) {
        return c5AccountMapper.deleteC5AccountByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除C5Game账户信息
     *
     * @param id C5Game账户主键
     * @return 结果
     */
    @Override
    public int deleteC5AccountById(Long id) {
        return c5AccountMapper.deleteC5AccountById(id);
    }

    @Override
    public int refreshAccountInfo(String appKey) {
        if (ObjectUtils.isEmpty(appKey)) {
            throw new RuntimeException("appKey不能为空");
        }
        List<C5Account> c5Accounts = c5AccountMapper.selectList(new LambdaQueryWrapper<C5Account>()
                .eq(C5Account::getAppKey, appKey));
        if (ObjectUtils.isEmpty(c5Accounts)) {
            throw new RuntimeException("没有找到appKey对应的C5账号");
        }
        for (C5Account c5Account : c5Accounts) {
            GetSteamInfoResponse response = c5ApiClientService.getSteamInfoByC5AppKey(c5Account.getAppKey());
            if (ObjectUtils.isEmpty(response)) {
                log.error(String.format("获取用户 %s steam信息失败，失败原因：请求C5接口/merchant/account/v1/steamInfo失败,返回信息为空", c5Account.getAppKey()));
            } else if (!response.getSuccess()) {
                log.error(String.format("获取用户 %s steam信息失败,失败原因：%s", c5Account.getAppKey(), response.getErrorMsg()));
            } else {
                GetSteamInfoResponse.GetSteamInfoData data = response.getData();
                if (!ObjectUtils.isEmpty(data) && !ObjectUtils.isEmpty(data.getSteamList())) {
                    for (GetSteamInfoResponse.SteamInfoVO steamInfoVO : data.getSteamList()) {
                        C5Account account = c5AccountMapper.selectOne(new LambdaQueryWrapper<C5Account>()
                                .eq(C5Account::getSteamId, steamInfoVO.getSteamId()));
                        if (ObjectUtils.isEmpty(account)) {
                            account = new C5Account();
                            account.setId(snowflakeIdGenerator.nextId());
                            account.setUserId(getUserId());
                            account.setUid(data.getUid());
                            account.setBalance((long) (data.getBalance() * 100L));
                            account.setAppKey(c5Account.getAppKey());
                            account.setSteamId(steamInfoVO.getSteamId());
                            account.setAvatar(steamInfoVO.getAvatar());
                            account.setC5Name(data.getNickname());
                            account.setCreateDate(new Date());
                            account.setUpdateDate(new Date());
                        } else {
                            account.setBalance((long) (data.getBalance() * 100L));
                            account.setAvatar(steamInfoVO.getAvatar());
                            account.setUid(data.getUid());
                            account.setC5Name(data.getNickname());
                            account.setUpdateDate(new Date());
                        }
                        c5AccountMapper.upsert(account);
                    }
                }
            }
        }
        return 1;
    }

    public static void main(String[] args) {
        C5ApiClientService c5ApiClientService = new C5ApiClientServiceImpl();
        GetSteamInfoResponse response = c5ApiClientService.getSteamInfoByC5AppKey("4bcb93f1681f49389c27007e85e0e70f");
        System.out.println(response);
    }
}
